<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '菜单添加' : '菜单编辑'"
      :visible.sync="info.isshow"
      @close="close"
    >
      <el-form :model="user">
        {{ user }}
        <el-form-item label=" 菜单名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" label-width="120px">
          <el-select
            v-model="user.pid"
            placeholder="请选择上级菜单"
            @change="changepid"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in arr"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label=" 菜单类型" label-width="120px">
          <el-radio v-model="user.type" disabled :label="1">目录</el-radio>
          <el-radio v-model="user.type" disabled :label="2">菜单</el-radio>
        </el-form-item>
        <!-- 目录出现图标 -->
        <el-form-item
          label="菜单图标"
          label-width="120px"
          v-if="user.type == 1"
        >
          <el-select v-model="user.icon" placeholder="请选择菜单图标">
            <el-option v-for="item in icons" :key="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 菜单出现地址 -->
        <el-form-item
          label="菜单地址"
          label-width="120px"
          v-if="user.type == 2"
        >
          <el-select v-model="user.url" placeholder="请选择菜单地址">
            <el-option
              v-for="item in IndexRouters"
              :key="item.path"
              :label="item.meta.title"
              :value="'/' + item.path"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRouters } from "../../../router";
import { reqmenuadd } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import {
  reqmenuedit,
  reqmenuinfo,
} from "../../../../../../day12/code/u_back/src/http/api";
export default {
  // 接收info
  props: ["info", "arr"],
  data() {
    // 5.1初始化表单的数据
    return {
      user: {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      // 5.2icon的集合
      icons: [
        "el-icon-delete",
        "el-icon-s-tools",
        "el-icon-user",
        "el-icon-goods",
        "el-icon-help",
        "el-icon-upload",
      ],
      // 5.3路由集合
      IndexRouters,
    };
  },
  computed: {
    ...mapGetters({}),
  },
  mounted() {},
  methods: {
    ...mapActions({}),
    // 8.点了取消
    cancel() {
      this.info.isshow = false;
    },
    // 清空弹窗的方法
    clearUser() {
      this.user = {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    // 5.4点了添加按钮
    add() {
      reqmenuadd(this.user).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // （2）清空弹窗
          this.clearUser();
          // （3）关闭弹窗
          this.info.isshow = false;
          // （4）添加一条数据，重新请求列表
          this.$emit("init");
        }
      });
    },
    // 6.6上级菜单发生了改变
    changepid() {
      //  如果上级菜单时 顶级菜单， 那么类型就是目录， 否则，类型就是菜单
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    // 8.编辑逻辑
    getOne(id) {
      // console.log("发请求"+id);
      reqmenuinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.user = res.data.list;
          // 补id
          this.user.id = id;
        }
      });
    },
    // 9.修改
    update() {
      reqmenuedit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.clearUser();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.$emit("init");
        }
      });
    },
    // 10.关闭弹框
    close() {
      if (!this.info.isAdd) {
        this.clearUser();
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>